package com.invengo.device;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.res.Resources;

import com.invengo.library_rfid.R;
import com.invengo.library_rfid.entity.ScanSettingMasterEntity;
import com.invengo.library_rfid.entity.ScanSettingTagReadPatternEntity;
import com.invengo.library_rfid.utils.PropertiesUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

import me.goldze.mvvmhabit.utils.cache.ACache;
import me.goldze.mvvmhabit.utils.cache.AKey;


public class ParamerManager {
    private static ParamerManager instance;

    public static ParamerManager getInstance() {
        if (instance == null) synchronized (ParamerManager.class) {
            if (instance == null) {
                instance = new ParamerManager();
            }
        }
        return instance;
    }


    /**
     * init默认配置
     *
     * @param application 应用
     */
    private Resources resources;

    public void initDefaultConfig(Application application) {
        resources = application.getResources();
        new Thread(() -> {
//            Object scanSettingMasterList = ACache.getInstance().getAsObject(AKey.RFID_SCAN_SETTING_MASTER_LIST);
//            if (scanSettingMasterList != null)
//                return;
            readDefaultReadConfig();
        }).start();
        initSupportedType(application);
    }

    private void initSupportedType(Application application) {
        Properties fixedConfig = PropertiesUtil.getProperties(application);
        int status6c = Integer.parseInt(fixedConfig.getProperty("tag6c"));
        int status6b = Integer.parseInt(fixedConfig.getProperty("tag6b"));
        int statusGb = Integer.parseInt(fixedConfig.getProperty("taggb"));
        int status6C6B = 0;
        if (status6c == 1 && status6b == 1) {
            status6C6B = 1;
        }
        int statusTemperature = Integer.parseInt(fixedConfig.getProperty("tagTemperature"));
        StringBuffer sb = new StringBuffer();
        if (status6c == 1) {
            sb.append("6C");
        }
        if (sb.length() > 0 && status6b == 1) {
            sb.append("/");
            sb.append("6B");
        } else if (sb.length() == 0 && status6b == 1) {
            sb.append("6B");
        }
        if (sb.length() > 0 && statusGb == 1) {
            sb.append("/");
            sb.append("GB");
        } else if (sb.length() == 0 && statusGb == 1) {
            sb.append("GB");
        }
        if (sb.length() > 0 && statusTemperature == 1) {
            sb.append("/");
            sb.append("Temp");
        } else {
            sb.append("Temp");
        }
        supportedType = sb.toString();
    }

    /**
     * 读取默认主配置
     */
    private void readDefaultMasterConfig(Map<Integer, List<ScanSettingTagReadPatternEntity>> collect) {

        List<String[]> masterParameter = new ArrayList<>(5);
        ArrayList<ScanSettingMasterEntity> params = new ArrayList<>();
        masterParameter.add(resources.getStringArray(R.array.master_6c_parameter));
        for (String[] parameter : masterParameter) {
            ScanSettingMasterEntity master = new ScanSettingMasterEntity();
            master.setVoice(Integer.parseInt(parameter[0]));
            master.setType(Integer.parseInt(parameter[1]));
            master.setAntenna(Integer.parseInt(parameter[2]));
            master.setName(parameter[3]);
            master.setCode(Integer.parseInt(parameter[4]));
            master.setPatterns(collect.get(master.getCode()));
            master.setFilter(Integer.parseInt(parameter[5]));
            master.setData(parameter[6]);
            master.setBank(Integer.parseInt(parameter[7]));
            master.setActive(Integer.parseInt(parameter[8]));
            master.setViewType(Integer.parseInt(parameter[9]));
            master.setSorting(Integer.parseInt(parameter[10]));
            params.add(master);
        }
        ACache.getInstance().put(AKey.RFID_SCAN_SETTING_MASTER_LIST, params);
    }

    /**
     * 读取默认读取配置
     */
    private void readDefaultReadConfig() {
        List<String[]> parameters = new ArrayList<String[]>(14);
        ArrayList<ScanSettingTagReadPatternEntity> tagReadList = new ArrayList<>();

        parameters.add(resources.getStringArray(R.array.read_epc_tid_parameter));
        parameters.add(resources.getStringArray(R.array.read_epc_parameter));
        parameters.add(resources.getStringArray(R.array.read_tid_parameter));
        parameters.add(resources.getStringArray(R.array.read_userdata_parameter));
        parameters.add(resources.getStringArray(R.array.read_epc_tid_userdata_parameter));
        parameters.add(resources.getStringArray(R.array.read_epc_userdata_parameter));
        parameters.add(resources.getStringArray(R.array.read_tid_userdata_parameter));

        for (String[] parameter : parameters) {
            ScanSettingTagReadPatternEntity pattern = new ScanSettingTagReadPatternEntity();
            pattern.setCode(Integer.parseInt(parameter[0]));
            pattern.setName(parameter[1]);
            pattern.setTagTypeCode(Integer.parseInt(parameter[2]));
            pattern.setEnabled(Integer.parseInt(parameter[3]));
            pattern.setAddress(Integer.parseInt(parameter[4]));
            pattern.setLen(Integer.parseInt(parameter[5]));
            pattern.setTidIdLen(Integer.parseInt(parameter[6]));
            pattern.setTarget(Integer.parseInt(parameter[7]));
            pattern.setSession(Integer.parseInt(parameter[8]));
            pattern.setConditional(Integer.parseInt(parameter[9]));
            pattern.setPassword(parameter[10]);
            pattern.setBank(Integer.parseInt(parameter[11]));
            pattern.setTidPassword(parameter[12]);
            pattern.setEpcLen(Integer.parseInt(parameter[13]));
            pattern.setUserBank(Integer.parseInt(parameter[14]));
            pattern.setUserBankPassword(parameter[15]);
            pattern.setTemperature(Integer.parseInt(parameter[16]));
            tagReadList.add(pattern);
        }
         Map<Integer, List<ScanSettingTagReadPatternEntity>> collect = tagReadList.stream().collect(Collectors.groupingBy(ScanSettingTagReadPatternEntity::getTagTypeCode));
        readDefaultMasterConfig(collect);
    }

    /**
     * 查询参数
     *
     * @return {@link ScanSettingMasterEntity}
     */
    public ScanSettingMasterEntity queryParameter() {
        // : 2023/10/31 这里还是数据库好用
        ArrayList<ScanSettingMasterEntity> settingMasterEntities = (ArrayList<ScanSettingMasterEntity>) ACache.getInstance().getAsObject(AKey.RFID_SCAN_SETTING_MASTER_LIST);
        ArrayList<ScanSettingTagReadPatternEntity> readPatternEntities = (ArrayList<ScanSettingTagReadPatternEntity>) ACache.getInstance().getAsObject(AKey.RFID_SCAN_SETTING_READ_LIST);
        if (settingMasterEntities == null)
            return null;
        for (ScanSettingMasterEntity settingMasterEntity : settingMasterEntities) {
            if (settingMasterEntity.getActive() == 0) {
                List<ScanSettingTagReadPatternEntity> temp = new ArrayList<>();
                for (ScanSettingTagReadPatternEntity readPatternEntity : readPatternEntities) {
                    if (settingMasterEntity.getCode() == readPatternEntity.getTagTypeCode())
                        temp.add(readPatternEntity);
                }
                settingMasterEntity.setPatterns(temp);
                return settingMasterEntity;
            }
        }
        return null;
    }

    private String supportedType;

    public String getSupportedType() {
        return supportedType;
    }
}
